package org.lq.ht.view.vip;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import org.lq.ht.entity.Vip;
import org.lq.ht.service.DictionaryService;
import org.lq.ht.service.VipService;
import org.lq.ht.service.impl.DictionaryServiceImpl;
import org.lq.ht.service.impl.VipServiceImpl;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class InitPassword extends JFrame {

	private JPanel contentPane;
	private JPasswordField passwordField;

	private JButton btnNewButton_1;
	
	private  static V_VipAdd add;
	public V_VipAdd getAdd() {
		return add;
	}
	private int vid;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InitPassword frame = new InitPassword();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
    public InitPassword( int vid) {
    	this();
    	this.vid = vid;
    }
	/**
	 * Create the frame.
	 */
	public InitPassword() {
		setTitle("输入密码");
		setAlwaysOnTop(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 293, 129);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("密码：");
		lblNewLabel.setBounds(21, 37, 54, 15);
		contentPane.add(lblNewLabel);

		passwordField = new JPasswordField();
		passwordField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==10) {
					String p =new String(passwordField.getPassword());
					VipService vs = new VipServiceImpl();
					String str = vs.getByVid(vid).getPassword();
					
					if(str.equals(p)) {
						//Vip修改
	                	add = new V_VipAdd(vid);
	                	add.setBounds(InitPassword.this.getX(), InitPassword.this.getY()-200, add.getWidth(), add.getHeight());
	                	add.setVisible(true);
	                	
	                	dispose();//自身窗口关闭
					}else {
						int v = JOptionPane.showConfirmDialog(InitPassword.this, "输入密码错误！是否重置！" );
						if(v==0) {
							down();
						}else {
							up();
						}
					}
				}
			}
		});
		passwordField.setBounds(62, 34, 110, 21);
		contentPane.add(passwordField);

		JButton btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String p =new String(passwordField.getPassword());
				VipService vs = new VipServiceImpl();
				String str = vs.getByVid(vid).getPassword();
				
				if(str.equals(p)) {
					//Vip修改
                	add = new V_VipAdd(vid);
                	add.setBounds(InitPassword.this.getX(), InitPassword.this.getY()-200, add.getWidth(), add.getHeight());
                	add.setVisible(true);
                	
                	dispose();//自身窗口关闭
				}else {
					int v = JOptionPane.showConfirmDialog(InitPassword.this, "输入密码错误！是否重置！" );
					if(v==0) {
						down();
					}
				}
			}
		});
		btnNewButton.setBounds(178, 33, 67, 23);
		contentPane.add(btnNewButton);

		btnNewButton_1 = new JButton("重置密码");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//密码重置
				int v = JOptionPane.showConfirmDialog(InitPassword.this, "请确认是否重置！" );
				if(v==0) {
					//密码重置
					VipService vs = new VipServiceImpl();
					Vip vip = vs.getByVid(vid);
					
					System.out.println(vip);
					vip.setPassword("666666");
					if(vs.updateVip(vip)) {
						JOptionPane.showMessageDialog(InitPassword.this, "初始化成功！\n初始化密码为666666" );
						up();
					}else {
						JOptionPane.showMessageDialog(InitPassword.this, "初始化密码失败！" );
					}
				}else {
					up();
				}
			}
		});
		btnNewButton_1.setBackground(Color.RED);
		btnNewButton_1.setBounds(95, 105, 100, 23);
		contentPane.add(btnNewButton_1);
	}

	//下降
	public void down() {
		int height = 130;
		while(height <= 200) {
			height+=5;
			this.setSize(this.getWidth(), height);
		}
	}
	//上升
	public void up() {
		int height = 300;
		while(height >=130) {
			height-=5;
			this.setSize(this.getWidth(), height);
		}
	}
}
